import {
  GET_LIST_DATA,
  LEFT_CLICK,
  ADD_SELECTI_ITEM,
  MINUS_SELECTI_ITEM,
  SHOW_CHOOSE_CONTENT
} from '../actions/actionType'
const initState = {
  listData: { food_spu_tags: [] },
  currentLeftIndex: 0,
  index: 0,
  showChoose: false
}
const getListData = (state, action) => {
  return {
    ...state,
    listData: action.obj.data || { food_spu_tags: [] }
  }
}

const leftClick = (state, action) => {
  return {
    ...state,
    currentLeftIndex: action.obj.currentLeftIndex
  }
}
const addSelectTime = (state, action) => {
  return {
    ...state,
    listData: dealWithSelectItem(state, action, ADD_SELECTI_ITEM)
  }
}
const minusSelectTime = (state, action) => {
  return {
    ...state,
    listData: dealWithSelectItem(state, action, MINUS_SELECTI_ITEM)
  }
}
const dealWithSelectItem = (state, action, type) => {
  let listData = state.listData
  let list = state.listData.food_spu_tags || []
  let currentItem =
    list[
      action.obj.outIndex === undefined
        ? state.currentLeftIndex
        : action.obj.outIndex
    ]
  if (type === ADD_SELECTI_ITEM) {
    currentItem.spus[action.obj.index].chooseCount++
  } else {
    currentItem.spus[action.obj.index].chooseCount--
  }

  let _listData = JSON.parse(JSON.stringify(listData))

  return _listData
}
const showChooseContent = (state, action) => {
  return {
    ...state,
    showChoose: action.obj.showChoose
  }
}
const menuReducer = (state = initState, action) => {
  switch (action.type) {
    case GET_LIST_DATA:
      return getListData(state, action)
    case LEFT_CLICK:
      return leftClick(state, action)
    case ADD_SELECTI_ITEM:
      return addSelectTime(state, action)
    case MINUS_SELECTI_ITEM:
      return minusSelectTime(state, action)
    case SHOW_CHOOSE_CONTENT:
      return showChooseContent(state, action)
    default:
      return state
  }
}
export default menuReducer
